//
//  FindPwdViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/2/28.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "FindPwdViewController.h"
#import "FindPwdView.h"
//验证码倒计时
#import "WLButtonCountdownManager.h"
#import "FindPwdModelPack.h"
#import "SMSModelPack.h"

@interface FindPwdViewController ()
@property(nonatomic,strong)FindPwdView *fv;

@property(nonatomic,strong) NSString *identifyKey;

@end

@implementation FindPwdViewController

-(instancetype)init{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    self.identifyKey = NSStringFromClass([self class]);
}

#pragma mark 初始化
-(void)loadView{
    [super loadView];
    self.fv = [[FindPwdView alloc] initWithFrame:self.view.bounds];
    self.view = self.fv;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[WLButtonCountdownManager defaultManager] countdownTaskExistWithKey:self.identifyKey task:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    self.view.backgroundColor = [UIColor whiteColor];
    //输入框有值可以点完成
    [self.fv.phonoNumTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.fv.securityCodeNumTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.fv.newpasswordNumTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.fv.confirmPasswordNumTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //发送验证码
    [self.fv.sendSMSBtn addTarget:self action:@selector(sendSMSBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    //完成
    [self.fv.finishBtn addTarget:self action:@selector(finishBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)finishBtnAction
{
    FindPwdRequest *request = [[FindPwdRequest alloc] init];
    request.mobile = self.fv.phonoNumTf.text;
    request.captcha = self.fv.securityCodeNumTf.text;
    request.newPassWord = [self.fv.newpasswordNumTf.text md5];
    request.confirmPassWord = [self.fv.confirmPasswordNumTf.text md5];
    NSDictionary *dic = [request mj_JSONObject];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/password/forgetPwdA0400"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:dic HttpBody:nil SuccessHandler:^(id responseObject) {
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [self FindPwdSuccessWithResponse:responseObject];
        }
        
        
    } FailedHandler:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)FindPwdSuccessWithResponse:(id )responseObject
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showSuccessResultTips:@"找回密码成功" animated:YES forSeconds:SUCCESS_SHOW_TIME_DEF completionBlock:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
    });
    
}

-(void)sendSMSBtnAction
{
    NSLog(@"点击发送验证码");
    
    if (![AppTools validateMobile:self.fv.phonoNumTf.text]) {
        [self showFailedResultTips:@"请输入正确的手机号" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    
    SMSRequest *request = [[SMSRequest alloc] init];
    request.mobile = self.fv.phonoNumTf.text;
    request.smsType = @"2";
    NSDictionary *dic = [request mj_JSONObject];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/captcha/sendCodeA0200"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:dic HttpBody:nil SuccessHandler:^(id responseObject) {
        NSLog(@"成功");
        [self dealSMSResponse:responseObject];
    } FailedHandler:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)dealSMSResponse:(id)responseObject
{
    __weak __typeof(self) weakSelf = self;
    [[WLButtonCountdownManager defaultManager] scheduledCountDownWithKey:self.identifyKey timeInterval:60 countingDown:^(NSTimeInterval leftTimeInterval) {
        weakSelf.fv.sendSMSBtn.userInteractionEnabled = NO;
        [weakSelf.fv.sendSMSBtn setTitle:[NSString stringWithFormat:@"%@%@",@(leftTimeInterval),@"s"] forState:(UIControlStateNormal)];
        [weakSelf.fv.sendSMSBtn setTitleColor:[UIColor colorWithRed:150 / 255.0 green:196 / 255.0 blue:255 / 255.0 alpha:1] forState:(UIControlStateNormal)];
    } finished:^(NSTimeInterval finalTimeInterval) {
        [weakSelf.fv.sendSMSBtn setTitle:@"再次发送" forState:(UIControlStateNormal)];
        [weakSelf.fv.sendSMSBtn setTitleColor:[UIColor colorWithRed:49.0f/255.0f green:138.0f/255.0f blue:255.0f/255.0f alpha:1.0f] forState:(UIControlStateNormal)];
        weakSelf.fv.sendSMSBtn.userInteractionEnabled = YES;
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.fv.phonoNumTf resignFirstResponder];
    [self.fv.securityCodeNumTf resignFirstResponder];
    [self.fv.newpasswordNumTf resignFirstResponder];
    [self.fv.confirmPasswordNumTf resignFirstResponder];
}

#pragma mark textField 监听
//输入框符合规则可以点击"下一步"
-(void)textFieldDidChange :(UITextField *)textField{
    
    //手机号输入框只能输入11位
    if (textField == self.fv.phonoNumTf) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
    
    //验证码输入框只能输入6位
    if (textField == self.fv.securityCodeNumTf) {
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
    
    
    
    if (self.fv.phonoNumTf.text.length > 0 &&
        self.fv.securityCodeNumTf.text.length > 0 &&
        self.fv.newpasswordNumTf.text.length > 0 &&
        self.fv.confirmPasswordNumTf.text.length > 0) {
        //验证码输入的是6位  手机号输入框是手机号
        self.fv.finishBtn.userInteractionEnabled = YES;
        self.fv.finishBtn.backgroundColor = ETCONFIRMCOLOR;
    }else
    {
        self.fv.finishBtn.userInteractionEnabled = NO;
        self.fv.finishBtn.backgroundColor = ETCOMMONCOLOR;
    }
}

@end
